package com.hisoft.web.view.report;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.lowagie.text.pdf.AcroFields;

public class ITextField {
	private AcroFields acroFields = null;

	private DecimalFormat df;

	public ITextField(AcroFields acroFields) {
		this.acroFields = acroFields;
		df = (DecimalFormat) NumberFormat.getInstance();
		df.applyPattern("0.00");
		df.setMaximumFractionDigits(2);
	}

	@SuppressWarnings("deprecation")
	public void setField(String name, Object value) throws Exception {
		if (null == value) {
			this.acroFields.setField(name, "");
			return;
		}
		if (value instanceof Integer) {
			this.acroFields.setField(name, "" + value);
		} else if (value instanceof Long || value instanceof Float
				|| value instanceof Double || value instanceof BigDecimal) {
			this.acroFields.setField(name, df.format(value));
		} else if (value instanceof Boolean) {
			Boolean data = (Boolean) value;
			this.acroFields.setField(name, data ? "yes" : "no");
		} else if (value instanceof Date) {
			Date date = (Date) value;
			if (date.compareTo(new Date(0, 0, 1)) != 0) {
				this.acroFields.setField(name, new SimpleDateFormat(
						"yyyy-MM-dd").format(date));
			} else {
				this.acroFields.setField(name, "");
			}
		} else {
			this.acroFields.setField(name, (String) value);
		}

	}
}
